package com.qms.tubu.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.qms.tubu.R;
import com.qms.tubu.activity.MultiPhotoActivity;
import com.qms.tubu.bean.DateBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mad on 2015/7/24.
 */
public class DateAdapter extends LoadMoreAdapter<DateBean> {
    private Activity mActivity;
    public DateAdapter(Activity activity) {
        super(activity);
        mActivity=activity;
    }

    @Override
    public int getItemCount() {
        return mList.size()+1;
    }

    @Override
    public int getItemViewType(int position) {
        if(position==mList.size())
           return super.getItemViewType(position);
        else
            return 0;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(getItemViewType(position)==0)
        {
            DateHolder dateHolder= (DateHolder) holder;
            final DateBean date=getList().get(position);
            dateHolder.nickname.setText(date.getNickname());
            dateHolder.address.setText(date.getAddress());
            dateHolder.createTime.setText(date.getCtime());
            dateHolder.discussNum.setText(String.valueOf(date.getDiscuss_times()));
            dateHolder.yueNum.setText(String.valueOf(date.getDate_times()));
            SpannableString contentStr=new SpannableString(date.getTime()+" "+date.getContent());
            contentStr.setSpan(new ForegroundColorSpan(mActivity.getResources().getColor(R.color.light_blue)),
                    0,date.getTime().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            dateHolder.content.setText(contentStr);
            Glide.with(mActivity).load(date.getHeadimgurl())
                    .centerCrop()
                    .crossFade()
                    .into(dateHolder.portraitImage);
            dateHolder.sexImage.setImageResource(date.getSex()==1?R.drawable.man_icon :R.drawable.woman_icon);
            if(!date.is_dated())
                dateHolder.yueIcon.setColorFilter(mActivity.getResources().getColor(R.color.light_gray));
            else
                dateHolder.yueIcon.clearColorFilter();
            for(int i=0;i<dateHolder.dateImageList.size();i++)
            {
                ImageView imageView=dateHolder.dateImageList.get(i);
                if(i<date.getImage().length)
                {
                    imageView.setVisibility(View.VISIBLE);
                    imageView.setOnClickListener(new OnPositionClickListener(date.getImage(),i));
                    Glide.with(mActivity).load(date.getImage()[i])
                            .centerCrop().crossFade().into(imageView);
                }
                else
                {
                    imageView.setVisibility(View.INVISIBLE);
                    imageView.setOnClickListener(null);
                }
            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType==0)
        {
            View view= LayoutInflater.from(mContent).inflate(R.layout.date_item,parent,false);
            return new DateHolder(view);
        }
        else
            return super.onCreateViewHolder(parent, viewType);
    }
    private class OnPositionClickListener implements View.OnClickListener{
        String[] mImages;
        int mPosition;
        public OnPositionClickListener(String[] images,int position)
        {
            mImages=images;
            mPosition=position;
        }
        @Override
        public void onClick(View v) {
            Intent intent=MultiPhotoActivity.getInstance(mActivity,mImages,mPosition);
            mActivity.startActivity(intent);
        }
    }
    private class DateHolder extends ClickViewHolder {
        private TextView nickname,content,address,createTime,discussNum,yueNum;
        private ImageView portraitImage,sexImage,yueIcon;
        private List<ImageView> dateImageList;
        public DateHolder(View itemView) {
            super(itemView);
            nickname= (TextView) itemView.findViewById(R.id.date_nickname_text);
            content= (TextView) itemView.findViewById(R.id.date_content_text);
            address= (TextView) itemView.findViewById(R.id.date_address_text);
            createTime= (TextView) itemView.findViewById(R.id.date_create_time_text);
            discussNum= (TextView) itemView.findViewById(R.id.date_discuss_count_text);
            yueNum= (TextView) itemView.findViewById(R.id.date_yue_count_text);
            portraitImage= (ImageView) itemView.findViewById(R.id.date_portrait_image);
            sexImage= (ImageView) itemView.findViewById(R.id.date_sex_image);
            yueIcon= (ImageView) itemView.findViewById(R.id.date_yue_icon);
            dateImageList=new ArrayList<ImageView>();
            LinearLayout layout= (LinearLayout) itemView.findViewById(R.id.date_image_panel);
            for(int i=0;i<layout.getChildCount();i++)
            {
                dateImageList.add((ImageView) layout.getChildAt(i));
            }
        }
    }
}
